<template>
  <div class="app-container">
    <div style="float:right;margin-bottom: 5px;">
      <el-radio v-model="timeRange" label="1">3天内</el-radio>
      <el-radio v-model="timeRange" label="2">7天内</el-radio>
      <el-radio v-model="timeRange" label="3">1个月内</el-radio>
    </div>
    <el-table :data="list" style="width: 100%;padding-top: 15px;">
      <el-table-column label="指标ID" width="100" align="center">
        <template slot-scope="scope">
          {{ scope.row.quotaId }}
        </template>
      </el-table-column>
      <el-table-column label="指标名" width="100" align="center">
        <template slot-scope="{row}">
          {{ row.name }}
        </template>
      </el-table-column>
      <el-table-column label="设备ID" min-width="100">
        <template slot-scope="scope">
          {{ scope.row.deviceId }}
        </template>
      </el-table-column>
      <el-table-column label="设备号" min-width="100">
        <template slot-scope="scope">
          {{ scope.row.number }}
        </template>
      </el-table-column>
      <el-table-column label="设备名" min-width="100">
        <template slot-scope="scope">
          {{ scope.row.deviceName }}
        </template>
      </el-table-column>
      <el-table-column label="报警次数" width="100" align="center">
        <template slot-scope="{row}">
          {{ row.heapValue }}
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getTop10Alarm } from '@/api/report'
import { parseTime } from '@/utils'

export default {
  data() {
    return {
      timeRange: '1',
      list: null
    }
  },
  watch: {
    'timeRange': {
      handler(newVal) {
        const query = this.getTimeRange(newVal)
        console.log('query', query)
        this.fetchData(query)
      }
    }
  },
  created() {
    const query = this.getTimeRange(this.timeRange)
    this.fetchData(query)
  },
  methods: {
    fetchData(query) {
      getTop10Alarm(query).then(response => {
        this.list = response.data.top
      })
    },
    getTimeRange(newVal) {
      let end = new Date().getTime()
      let dur = 0
      if (newVal === '1') dur += 1000 * 60 * 60 * 24 * 3
      else if (newVal === '2') dur += 1000 * 60 * 60 * 24 * 7
      else if (newVal === '3') dur += 1000 * 60 * 60 * 24 * 30
      let beg = end - dur
      const cFormat = '{y}-{m}-{d}T{h}:{i}:{s}Z'
      beg = parseTime(end - dur, cFormat)
      end = parseTime(end, cFormat)
      // console.log('timeRange', beg, end)
      return { beg, end }
    }
  }
}
</script>
